class AttachmentsController < ApplicationController
  def form_for
    num = params[:num] || 0
    type = params[:type] || 'generic'

    dom_id = "attachment_#{num}"
    partial = case type
      when 'Image'        then 'shared/attach_image_form'
      when 'PatchFile'    then 'shared/attach_patch_form'
      when 'GenericFile'  then 'shared/attach_generic_file_form'
      when 'IssueRef'     then 'shared/attach_issue_form'
      when 'CommentRef'   then 'shared/attach_comment_form'
      else 'shared/attach_generic_file_form'
    end

    if request.xhr?
      respond_to do |format|
        format.js do
          render :update do |page|
            page.replace_html( dom_id,
                  :partial => partial,
                  :locals => { :attachment_name => dom_id }
                )
            page.insert_html( :after, dom_id,
                  :partial => 'shared/attach',
                  :locals => { :num => (num.to_i + 1) }
                )
          end
        end
      end
    else
      redirect_to :welcome
    end
  end

  # GET /attachments
  # GET /attachments.xml
  def index
    @attachments = Attachment.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @attachments }
    end
  end

  # GET /attachments/1
  # GET /attachments/1.xml
  def show
    @attachment = Attachment.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @attachment }
    end
  end

  # GET /attachments/new
  # GET /attachments/new.xml
  def new
    @attachment = Attachment.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @attachment }
    end
  end

  # GET /attachments/1/edit
  def edit
    @attachment = Attachment.find(params[:id])
  end

  # POST /attachments
  # POST /attachments.xml
  def create
    @attachment = Attachment.new(params[:attachment])

    respond_to do |format|
      if @attachment.save
        flash[:notice] = 'Attachment was successfully created.'
        format.html { redirect_to(@attachment) }
        format.xml  { render :xml => @attachment, :status => :created, :location => @attachment }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @attachment.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /attachments/1
  # PUT /attachments/1.xml
  def update
    @attachment = Attachment.find(params[:id])

    respond_to do |format|
      if @attachment.update_attributes(params[:attachment])
        flash[:notice] = 'Attachment was successfully updated.'
        format.html { redirect_to(@attachment) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @attachment.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /attachments/1
  # DELETE /attachments/1.xml
  def destroy
    @attachment = Attachment.find(params[:id])
    @attachment.destroy

    respond_to do |format|
      format.html { redirect_to(attachments_url) }
      format.xml  { head :ok }
    end
  end
end
